const databasePool = require('../util/databasePool')

const userDao = {
    getUserByUname(userName){ //根据用户名获取用户
        return new Promise((resolve,reject) => {
            const sql = 'SELECT * FROM `user` where userName = ?'
            databasePool.query({
                sql,
                props:[userName]
            })
            .then(res => {
                resolve(res)  //返回正确的结果
            })
            .catch(err => {
                reject(err) 
            })
        })
    },
    addUserToken(props){ // 给用户添加token
        return new Promise((resolve,reject) => {
            const sql = 'UPDATE `user` set token=? WHERE userName=?'
            databasePool.query({
                sql,
                props
            })
            .then(res => {
                resolve(res)
            })
            .catch(err => {
                reject(err)
            })
        })
    },
    getData({searchSql,props}){
        return new Promise((resolve,reject) => {
            databasePool.query({
                sql:searchSql,
                props
            })
            .then(res => {
                resolve(res)
            })
            .catch(err => {
                reject(err)
            })
        })
    }
}

module.exports = userDao